<?php

class DiaryBodyModel
{
    private $diaryBodyKeyPrefix =   'DB/';

    public function addDiaryBody($diaryInfo)
    {
        if (empty($diaryInfo['uid']))
        {
            AC::$errorNo    =   'DBM001';
            return  FALSE;
        }
        $data['uid']        =   $diaryInfo['uid'];
        $data['saveTime']   =   AC::SERVER_TIME;

        isset($diaryInfo['saveTime'])   &&  $data['saveTime']   =   $diaryInfo['saveTime'];
        // 截取日记长度
        isset($diaryInfo['content'])    &&  $data['content']    =   $diaryInfo['content'];
        isset($diaryInfo['category'])   &&  $data['category']   =   abs($diaryInfo['category']);
        isset($diaryInfo['secret'])     &&  $data['secret']     =   abs($diaryInfo['secret']);
        isset($diaryInfo['weather'])    &&  $data['weather']    =   abs($diaryInfo['weather']);
        isset($diaryInfo['emotion'])    &&  $data['emotion']    =   abs($diaryInfo['emotion']);
        // 字体最大限制
        $textSize = intval($diaryInfo['textSize']) > AC::DIARY_TEXT_SIZE_MAXID ? AC::DIARY_TEXT_SIZE_MAXID : abs($diaryInfo['textSize']);
        isset($diaryInfo['textSize'])   &&  $data['textSize']   =   abs($textSize);
        isset($diaryInfo['textColor'])  &&  $data['textColor']  =   $diaryInfo['textColor'];
        isset($diaryInfo['theme'])      &&  $data['theme']      =   abs($diaryInfo['theme']);
        isset($diaryInfo['posX'])       &&  $data['posX']       =   $diaryInfo['posX'];
        isset($diaryInfo['posY'])       &&  $data['posY']       =   $diaryInfo['posY'];
        isset($diaryInfo['font'])       &&  $data['place']      =   $diaryInfo['font'];
        isset($diaryInfo['paper'])      &&  $data['paper_url']  =   $diaryInfo['paper'];
        isset($diaryInfo['extend'])     &&  $data['extend']     =   $diaryInfo['extend'];
        isset($diaryInfo['diaryType'])  &&  $data['inReview']   =   $diaryInfo['diaryType'];
	isset($diaryInfo['vote'])       &&  $data['vote']       =   $diaryInfo['vote'];

        $key    =   $this->diaryBodyKeyPrefix. $diaryInfo['uid'];
        $result =   Storage::addData($key, $data);

        if (empty($result))
        {
            AC::$errorNo    =   'DBM002';
            return  FALSE;
        }
        return  $result;
    }

	public function updateDiaryBody($diaryInfo,$diaryId){
		if (empty($diaryInfo['uid']) || !$diaryId)
		{
			AC::$errorNo    =   'DBM001';
			return  false;
		}

		$where = array('id'=>$diaryId);

		$data['uid']        =   $diaryInfo['uid'];

		isset($diaryInfo['saveTime'])   &&  $data['saveTime']   =   $diaryInfo['saveTime'];
		// 截取日记长度
		isset($diaryInfo['content'])    &&  $data['content']    =   $diaryInfo['content'];
		isset($diaryInfo['category'])   &&  $data['category']   =   abs($diaryInfo['category']);
		isset($diaryInfo['secret'])     &&  $data['secret']     =   abs($diaryInfo['secret']);
		isset($diaryInfo['weather'])    &&  $data['weather']    =   abs($diaryInfo['weather']);
		isset($diaryInfo['emotion'])    &&  $data['emotion']    =   abs($diaryInfo['emotion']);
		// 字体最大限制
		$textSize = intval($diaryInfo['textSize']) > AC::DIARY_TEXT_SIZE_MAXID ? AC::DIARY_TEXT_SIZE_MAXID : abs($diaryInfo['textSize']);
		isset($diaryInfo['textSize'])   &&  $data['textSize']   =   abs($textSize);
		isset($diaryInfo['textColor'])  &&  $data['textColor']  =   $diaryInfo['textColor'];
		isset($diaryInfo['theme'])      &&  $data['theme']      =   abs($diaryInfo['theme']);
		isset($diaryInfo['posX'])       &&  $data['posX']       =   $diaryInfo['posX'];
		isset($diaryInfo['posY'])       &&  $data['posY']       =   $diaryInfo['posY'];
		isset($diaryInfo['font'])       &&  $data['place']      =   $diaryInfo['font'];
		isset($diaryInfo['paper'])      &&  $data['paper_url']  =   $diaryInfo['paper'];
		isset($diaryInfo['extend'])     &&  $data['extend']     =   $diaryInfo['extend'];
		isset($diaryInfo['diaryType'])  &&  $data['inReview']   =   $diaryInfo['diaryType'];
		isset($diaryInfo['vote'])       &&  $data['vote']       =   $diaryInfo['vote'];

		$key    =   $this->diaryBodyKeyPrefix. $diaryInfo['uid'];
		$result =   Storage::setConditionData($key, $data,$where);

		if (empty($result))
		{
			AC::$errorNo    =   'DBM002';
			return  false;
		}
		return  $result;
	}
    public function getBodyId($uid, $diaryId)
    {
        $key    =   $this->diaryBodyKeyPrefix. $uid;
        $bodyId =   UK::getBodyidByItemid($key, $diaryId);

        if (empty($bodyId))
        {
            AC::$errorNo    =   'DBM003';
            return  false;
        }
        return  $bodyId;
    }

    public function getDiaryId($bodyId)
    {
        $info   =   UK::getItemidByBodyid($bodyId);
        if (empty($info['itemId']))
        {
            AC::$errorNo    =   'DBM005';
            return  FALSE;
        }
        return  $info['itemId'];
    }

    public function getDiaryBody($uid, $bodyId)
    {
        if (empty($uid) ||  empty($bodyId))
        {
            AC::$errorNo    =   'DBM004';
            return  FALSE;
        }
        $diaryId=   $this->getDiaryId($bodyId);
        if (empty($diaryId))    return  FALSE;

        $where  =   array('id' => $diaryId, 'uid' => $uid, 'status' => 0);
        $key    =   $this->diaryBodyKeyPrefix. $uid;
        $result =   Storage::getConditionData($key, $where);
        return  $result;
    }

    public function setDiaryBody($diaryInfo)
    {
        if (empty($diaryInfo['uid']) || empty($diaryInfo['bodyId']))
        {
            AC::$errorNo    =   'DBM008';
            return  FALSE;
        }
        $diaryId    =   $this->getDiaryId($diaryInfo['bodyId']);
        if (empty($diaryId))    return  FALSE;

//         isset($diaryInfo['content'])    &&  $data['content']    =   $diaryInfo['content'];
        isset($diaryInfo['category'])   &&  $data['category']   =   abs($diaryInfo['category']);
        isset($diaryInfo['secret'])     &&  $data['secret']     =   abs($diaryInfo['secret']);
        isset($diaryInfo['weather'])    &&  $data['weather']    =   abs($diaryInfo['weather']);
        isset($diaryInfo['emotion'])    &&  $data['emotion']    =   abs($diaryInfo['emotion']);
        isset($diaryInfo['textSize'])   &&  $data['textSize']   =   abs($diaryInfo['textSize']);
        isset($diaryInfo['textColor'])  &&  $data['textColor']  =   $diaryInfo['textColor'];
        isset($diaryInfo['theme'])      &&  $data['theme']      =   abs($diaryInfo['theme']);
        isset($diaryInfo['posX'])       &&  $data['posX']       =   $diaryInfo['posX'];
        isset($diaryInfo['posY'])       &&  $data['posY']       =   $diaryInfo['posY'];
        isset($diaryInfo['place'])      &&  $data['place']      =   $diaryInfo['place'];
        isset($diaryInfo['inReview'])   &&  $data['inReview']   =   $diaryInfo['inReview'];
        isset($diaryInfo['saveTime'])   &&  !empty($diaryInfo['saveTime'])  &&  $data['saveTime']   =   $diaryInfo['saveTime'];

        $key    =   $this->diaryBodyKeyPrefix. $diaryInfo['uid'];
        $where  =   array('id' => $diaryId);
        $result =   Storage::setConditionData($key, $data, $where);

        if (empty($result))
        {
            AC::$errorNo    =   'DBM009';
            return  FALSE;
        }
        return  $result;
    }

    public function removeDiaryBody($diaryInfo)
    {
        if (empty($diaryInfo['uid']) || empty($diaryInfo['bodyId']))
        {
            AC::$errorNo    =   'DBM006';
            return  FALSE;
        }
        $diaryId=   $this->getDiaryId($diaryInfo['bodyId']);
        if (empty($diaryId))    return  FALSE;

        $data   =   array('status' => 1, 'saveTime' => AC::SERVER_TIME);
        $where  =   array('id' => $diaryId);
        $key    =   $this->diaryBodyKeyPrefix. $diaryInfo['uid'];
        $result =   Storage::setConditionData($key, $data, $where);
        if (empty($result))
        {
            AC::$errorNo    =   'DBM007';
        }
        //$this->delSquare($diaryInfo);
        return  $result;
    }
    
    // 删除广场
    public function delSquare($info){
        
        $key    =   'HS/0';
        $where  =   array('bodyId'  =>  $info['bodyId'], 'eUid' => $info['uid']);
        $data['status']  = '2';
        $result =   Storage::setConditionData($key, $data, $where);
    
        return  $result;
    }
    
}


